#!/bin/sh
########################################################################
# setup.sh - XXPROGNAMEXX installation script
# Copyright (C) 2013 XXAUTHORNAMEXX <XXAUTHOREMAILXX>
#
# These comments cannot be remove or edited and should remain intact
# because this file comments is an extension to the GPL3 license.
# This software file is covered by GPL version 3 license.
# Please see the LICENSE and gpl3.txt or gpl3.htm in doc directory
# for the license agreement, if its not exist, write to the 
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
# Modification below this line is allowed
########################################################################
# Extra credits to:
#
########################################################################
# Notes:
# 
########################################################################
echo ----------------------------------- [ XXPROGNAMEXX Setup ]
function GetFirstOrder()
{
	#cannot have IFS redefined in the wide env
	#it will screw the running path.
	#possibly ifs not restored correctly. - afifio
	local ifssaved=$IFS; IFS=":";
	FirstOrder $1
	IFS=$ifsaved
};export -f GetFirstOrder
function FirstOrder()
{
	echo $1
};export -f FirstOrder
ROOT_UID=0             # Root has $UID 0.
if [ "$UID" -ne "$ROOT_UID" ]
then
	if [ -n "$XDG_DATA_HOME" ]
	then
		INSTALLPATH=`GetFirstOrder $XDG_DATA_HOME`/XXPROGNAMEXX
	else
		INSTALLPATH=$HOME/.local/share/XXPROGNAMEXX
	fi
	echo "User specific installation into " $INSTALLPATH
elif [ "$UID" = "$ROOT_UID" ]
then
	if [ -n "$XDG_DATA_DIRS" ]
	then
		INSTALLPATH=`GetFirstOrder $XDG_DATA_DIRS`/XXPROGNAMEXX
	else
		INSTALLPATH=/usr/share/XXPROGNAMEXX
	fi
	echo "Systemwide installation into " $INSTALLPATH
fi
	PATHNOW=`pwd`
	CONFIGPATH=$HOME/.config/XXPROGNAMEXX
	CP="cp --update --recursive "
	LINKING="ln --force --symbolic "
	CHMOD="chmod --recursive "
	# "echo linking... "$1" to "$2"; ln -s"
	MKDIR="mkdir -p "
	echo "Start copying the executable"
	$CP XXPROGNAMEXX /usr/bin/XXPROGNAMEXX
	$LINKING /usr/bin/XXPROGNAMEXX /usr/local/bin/XXPROGNAMEXX
	
	echo "Creating the shared install dirs..."
	$MKDIR $INSTALLPATH
	$MKDIR $INSTALLPATH/bin
	$MKDIR $INSTALLPATH/data
	$MKDIR $INSTALLPATH/lib
	$MKDIR $INSTALLPATH/pix
	$MKDIR $INSTALLPATH/dlg
	$MKDIR $INSTALLPATH/doc
	$MKDIR $INSTALLPATH/locale
	$MKDIR $INSTALLPATH/pix
	$MKDIR $INSTALLPATH/xml
	$MKDIR /usr/local/apps/XXPROGNAMEXX
	
	echo "Copying the files..."
	$CP $PATHNOW/bin/ $INSTALLPATH
	$CP $PATHNOW/data/ $INSTALLPATH
	$CP $PATHNOW/lib/ $INSTALLPATH
	$CP $PATHNOW/pix/ $INSTALLPATH
	$CP $PATHNOW/dlg/ $INSTALLPATH
	$CP $PATHNOW/doc/ $INSTALLPATH
	$CP $PATHNOW/locale/ $INSTALLPATH
	$CP $PATHNOW/pix/ $INSTALLPATH
	$CP $PATHNOW/xml/ $INSTALLPATH
	$CP $PATHNOW/dist/ /usr/share/icons/
	
	echo "Desktop system awareness..."
	$LINKING $INSTALLPATH/bin/AppRun /usr/local/apps/XXPROGNAMEXX/AppRun
	$LINKING $INSTALLPATH/bin/AppInfo.xml /usr/local/apps/XXPROGNAMEXX/AppInfo.xml
	$LINKING $INSTALLPATH/bin/XXPROGNAMEXX.desktop /usr/share/applications/XXPROGNAMEXX.desktop
	$LINKING $INSTALLPATH/pix/XXPROGICONXX.svg /usr/share/icons/XXPROGICONXX.svg
	$LINKING $INSTALLPATH/pix/XXPROGICONXX.svg /usr/share/icons/hicolor/48x48/apps/XXPROGICONXX.svg
	$LINKING $INSTALLPATH/pix/XXPROGICONXX.svg /usr/local/apps/XXPROGNAMEXX/.DirIcon
	$LINKING $INSTALLPATH/pix/XXPROGICONXX.svg /usr/local/lib/X11/pixmaps/XXPROGICONXX.svg
	$LINKING $INSTALLPATH/pix/XXPROGICONXX.svg /usr/share/pixmaps/XXPROGICONXX.svg
	$LINKING --no-dereference $INSTALLPATH/doc /usr/share/doc/XXPROGNAMEXX
	echo "Permissions..."
	$CHMOD 755 /usr/bin/XXPROGNAMEXX
	$CHMOD 755 /usr/local/bin/XXPROGNAMEXX
	$CHMOD 644 $INSTALLPATH
	$CHMOD 755 $INSTALLPATH/lib
	$CHMOD 755 $INSTALLPATH/bin
	echo "User config and runtime files..."
	$MKDIR $CONFIGPATH
	$MKDIR $CONFIGPATH/tmp
	$MKDIR $CONFIGPATH/data
echo "Type here what you want, use it as a notice/scratch board" > $CONFIGPATH/data/notepad.txt
echo "#!/bin/bash" > $CONFIGPATH/data/script.sh
echo "<span color=\"Navy\" font-family=\"Sans\" size=\"large\"><i>XXAUTHORNAMEXX</i></span>" > $CONFIGPATH/data/textpad.txt
echo "XXPROGNAMEXX ready" > $CONFIGPATH/tmp/error
echo "XXPROGNAMEXX ready" > $CONFIGPATH/tmp/warning
echo "XXPROGNAMEXX ready" > $CONFIGPATH/tmp/question
echo "XXPROGNAMEXX ready" > $CONFIGPATH/tmp/info
echo "XXPROGNAMEXX ready" > $CONFIGPATH/tmp/authentication
echo gtk-apply " | "`date +%X` "| XXPROGNAMEXX installation done| XXPROGNAMEXX Setup" > $CONFIGPATH/data/log
echo "XXPROGNAMEXX now installed, executing..."
XXPROGNAMEXX
